package com.inews.base.utils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Map;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.springframework.security.core.context.SecurityContextHolder;

import com.inews.base.exception.BusinessException;
import com.inews.base.security.vo.BaseUserVo;

/**
 * 常见的辅助类
 * 
 * @author XiongChun
 * @since 2009-07-15
 */
public class InewsUtils {

	/**
	 * 判断对象是否Empty(null或元素为0)<br>
	 * 实用于对如下对象做判断:String Collection及其子类 Map及其子类
	 * 
	 * @param pObj
	 *            待检查对象
	 * @return boolean 返回的布尔值
	 */
	public static boolean isEmpty(Object pObj) {
		if (pObj == null)
			return true;
		if (pObj == "")
			return true;
		if (pObj instanceof String) {
			if (((String) pObj).length() == 0) {
				return true;
			}
		} else if (pObj instanceof Collection) {
			if (((Collection) pObj).size() == 0) {
				return true;
			}
		} else if (pObj instanceof Map) {
			if (((Map) pObj).size() == 0) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 判断对象是否为NotEmpty(!null或元素>0)<br>
	 * 实用于对如下对象做判断:String Collection及其子类 Map及其子类
	 * 
	 * @param pObj
	 *            待检查对象
	 * @return boolean 返回的布尔值
	 */
	public static boolean isNotEmpty(Object pObj) {
		if (pObj == null)
			return false;
		if (pObj == "")
			return false;
		if (pObj instanceof String) {
			if (((String) pObj).length() == 0) {
				return false;
			}
		} else if (pObj instanceof Collection) {
			if (((Collection) pObj).size() == 0) {
				return false;
			}
		} else if (pObj instanceof Map) {
			if (((Map) pObj).size() == 0) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 获得简单用户信息。（用于后台的操作） 登录时，不能使用此方法来获得用户信息。.
	 * 
	 * @return 获得用户信息
	 * @throws Exception
	 * @throws Exception
	 */
	public static BaseUserVo getUserInfo() {
		BaseUserVo user = null;
		try {
			user = (BaseUserVo) SecurityContextHolder.getContext()
					.getAuthentication().getPrincipal();
		} catch (Exception e) {
			throw new BusinessException("用户信息丢失，请重新登录");
		}
		return user;
	}

	public static String getCurrTime() {
		return DateFormatUtils.format(new Date(), "yyyyMMddHHmmss");
	}

	/**
	 * 数据格式化.
	 * 
	 * @param pattern
	 *            the pattern
	 * @param i
	 *            the i
	 * @return the string
	 */
	public static String codeFormat(String pattern, Object value) {
		DecimalFormat df = new DecimalFormat(pattern);
		return df.format(value);
	}

	/**
	 * 格式化时间.
	 * 
	 * @param date
	 *            the date
	 * @return the string
	 */
	public static String fomatDate(String date) {
		if (isNotEmpty(date)) {
			return date.substring(0, 4) + "-" + date.substring(4, 6) + "-"
					+ date.substring(6, 8);
		}
		return null;
	}

	/**
	 * 格式化时间.
	 * 
	 * @param date
	 *            the date
	 * @return the string
	 */
	public static String fomatLongDate(String date) {
		if (isNotEmpty(date)) {
			return date.substring(0, 4) + "-" + date.substring(4, 6) + "-"
					+ date.substring(6, 8) + " " + date.substring(8, 10) + ":"
					+ date.substring(10, 12) + ":" + date.substring(12, 14);
		}
		return null;
	}

	public static String fomatDate2String(String date) {
		if (isNotEmpty(date)) {
			return date.replace("T", "").replace("-", "").replace(" ", "")
					.substring(0, 8);
		}
		return null;
	}

	/**
	 * 格式化金额.
	 * 
	 * @param value
	 *            the value
	 * @return the string
	 */
	public static String formatCurrency2String(Long value) {
		if (value == null || "0".equals(String.valueOf(value))) {
			return "0.00";
		}
		DecimalFormat df = new DecimalFormat("0.00");
		return df.format(value / 100.00);
	}

	/**
	 * 格式化金额.
	 * 
	 * @param priceFormat
	 *            the price format
	 * @return the long
	 */
	public static Long formatCurrency2Long(String priceFormat) {
		if (InewsUtils.isNotEmpty(priceFormat)) {
			BigDecimal bg = new BigDecimal(priceFormat);
			Long price = bg.multiply(new BigDecimal(100)).longValue();
			return price;
		}
		return 0L;
	}

	public static String getValue(HSSFCell cell) {
		String value = "";

		if (isNotEmpty(cell)) {
			int cellType = cell.getCellType();
			switch (cellType) {

			case HSSFCell.CELL_TYPE_STRING:
				value = cell.getStringCellValue();
				break;

			case HSSFCell.CELL_TYPE_NUMERIC:
				if (HSSFDateUtil.isCellDateFormatted(cell)) {
					Date date = cell.getDateCellValue();
					if (date != null) {
						value = new SimpleDateFormat("yyyy-MM-dd").format(date);
					} else {
						value = "";
					}
				} else {
					value = new DecimalFormat("0").format(cell
							.getNumericCellValue());
				}
				break;

			case HSSFCell.CELL_TYPE_FORMULA:
				// 导入时如果为公式生成的数据则无值
			
					value = cell.getNumericCellValue() + "";
				

				break;

			case HSSFCell.CELL_TYPE_BLANK:
				break;

			case HSSFCell.CELL_TYPE_ERROR:
				value = "";
				break;

			case HSSFCell.CELL_TYPE_BOOLEAN:
				value = (cell.getBooleanCellValue() == true ? "true" : "false");
				break;

			default:
				value = "";
			}
		}

		return value;
	}

}